package com.chapter.sample.redis.controller;

import com.chapter.sample.entity.member.MemberEntity;
import com.chapter.sample.redis.service.CustomerService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Random;

/**
 * 客户Customer接口 + 布隆过滤器
 */
@RestController
@Slf4j
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 数据库初始2条记录
     *
     * @param member
     */
    @RequestMapping("/customer/add")
    public void addCustomer() {

        for (int i = 1; i <= 2; i++) {
            MemberEntity member = new MemberEntity();
            member.setMobile("1348871xxx" + i);
            member.setNickname("张三");
            member.setGender(new Random().nextInt(2));
            member.setBirth(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
            customerService.addCustomer(member);
        }

    }

    /**
     * 单个查询操作
     *
     * @param id
     * @return
     */
    @RequestMapping("/customer/{id}")
    public MemberEntity findMemberById(@PathVariable("id") Integer id) {
        return customerService.findMemberById(id);
    }

    @RequestMapping("/customerbloomfilter/{id}")
    public MemberEntity findMemberByIdWithBloomFilter(@PathVariable("id") Integer id) {
        return customerService.findMemberByIdWithBloomFilter(id);
    }
}
